package com.zxbit.flash.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.utils.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 首页弹框的
 */
public class HomeOneDialog extends Dialog {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvContent)
    TextView tvContent;
    @BindView(R.id.tvReason)
    TextView tvReason;
    @BindView(R.id.tvButton)
    TextView tvButton;

    private String title;
    private String content;
    private String eason;
    private String buttonStr;

    private Context mContext;
    private OneClickListener clickListener;

    public HomeOneDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_homeone, null);
        this.setContentView(view);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        // 设置对话框大小
        params.width = mContext.getResources().getDimensionPixelOffset(R.dimen.dialog_width);
        window.setAttributes(params);

        setCancelable(false);
        setCanceledOnTouchOutside(false);

        if (StringUtils.isNotBlank(title)) {
            tvTitle.setText(title);
        }
        if (StringUtils.isNotBlank(content)) {
            tvContent.setText(content);
        }
        if (StringUtils.isNotBlank(eason)) {
            tvReason.setText(eason);
            tvReason.setVisibility(View.VISIBLE);
        } else {
            tvReason.setVisibility(View.GONE);
        }
        if (StringUtils.isNotBlank(buttonStr)) {
            tvButton.setText(buttonStr);
        }
        tvButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (clickListener != null)
                    clickListener.onConfirm();
            }
        });
    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getEason() {
        return eason;
    }

    public void setEason(String eason) {
        this.eason = eason;
    }

    public String getButtonStr() {
        return buttonStr;
    }

    public void setButtonStr(String buttonStr) {
        this.buttonStr = buttonStr;
    }

    public HomeOneDialog setOneClickListener(OneClickListener listener) {
        clickListener = listener;
        return this;
    }

    public interface OneClickListener {
        void onConfirm();
    }

}
